﻿using System;
using System.Net;
using WeiboSharp.Web;

namespace WeiboSharp
{
    public partial class WeiboClient
    {
        public WeiboResponse Excute(WeiboRequest request)
        {
            request = AuthenticateIfNeeded(request);

            var response = new WeiboResponse();
            try
            {
                response = GetResponse(request);
                response.Request = request;
                response.Request.IncreaseNumAttempts();

            }
            catch (Exception ex)
            {
                response.StatusCode = HttpStatusCode.InternalServerError;
                response.ErrorMessage = ex.Message;
                response.ErrorException = ex;
            }

            return response;
        }

        private WeiboResponse GetResponse(WeiboRequest request)
        {
            IHttp http = new Http();
            http = ConfigureHttp(request, http);
            http= ConfigureProxy(http);

            var httpResponse = new WeiboResponse();

            switch (request.Method)
            {
                case WebMethod.Get:
                    httpResponse = http.Get();
                    break;
                case WebMethod.Post:
                    httpResponse = http.Post();
                    break;
                case WebMethod.Put:
                    httpResponse = http.Put();
                    break;
                case WebMethod.Delete:
                    httpResponse = http.Delete();
                    break;
                case WebMethod.Head:
                    httpResponse = http.Head();
                    break;
                case WebMethod.Options:
                    httpResponse = http.Options();
                    break;
            }
            return httpResponse;
        }
    }
}
